/*
 *进货管理控制器
 *整合进货订单新增所需的所有API方法
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.demo.IServices;

namespace VOL.demo.Controllers
{
    [Route("api/InboundManagement")]
    [ApiController]
    public class InboundManagementController : ControllerBase
    {
        private readonly IPurchaseOrderModelService _purchaseOrderService;
        private readonly IsuppliermodelService _supplierService;
        private readonly IproductinformationmodelService _productService;
        private readonly IunitmodelService _unitService;
        private readonly ISys_UserService _userService;
        private readonly ISys_DepartmentService _departmentService;
        private readonly IPurchaseOrderItemModelService _purchaseOrderItemService;

        public InboundManagementController(
            IPurchaseOrderModelService purchaseOrderService,
            IsuppliermodelService supplierService,
            IproductinformationmodelService productService,
            IunitmodelService unitService,
            ISys_UserService userService,
            ISys_DepartmentService departmentService,
            IPurchaseOrderItemModelService purchaseOrderItemService)
        {
            _purchaseOrderService = purchaseOrderService;
            _supplierService = supplierService;
            _productService = productService;
            _unitService = unitService;
            _userService = userService;
            _departmentService = departmentService;
            _purchaseOrderItemService = purchaseOrderItemService;
        }

        #region 供应商相关API

        /// <summary>
        /// 获取供应商选项
        /// </summary>
        /// <returns>供应商列表</returns>
        [HttpGet("GetSupplierOptions")]
        public async Task<IActionResult> GetSupplierOptions()
        {
            try
            {
                var suppliers = await _supplierService.GetSupplierOptionsAsync();
                return Ok(new { success = true, data = suppliers, message = "查询成功" });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 根据供应商ID获取供应商信息
        /// </summary>
        /// <param name="supplierId">供应商ID</param>
        /// <returns>供应商信息</returns>
        [HttpGet("GetSupplierById")]
        public async Task<IActionResult> GetSupplierById(long supplierId)
        {
            try
            {
                var supplier = await _supplierService.GetSupplierByIdAsync(supplierId);
                if (supplier != null)
                {
                    return Ok(new { success = true, data = supplier, message = "查询成功" });
                }
                else
                {
                    return Ok(new { success = false, message = "供应商不存在" });
                }
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        #endregion

        #region 用户相关API

        /// <summary>
        /// 获取用户选项
        /// </summary>
        /// <returns>用户列表</returns>
        [HttpGet("GetUserOptions")]
        public async Task<IActionResult> GetUserOptions()
        {
            try
            {
                var users = await _userService.GetUserOptionsAsync();
                return Ok(new { success = true, data = users, message = "查询成功" });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        #endregion

        #region 部门相关API

        /// <summary>
        /// 获取部门选项
        /// </summary>
        /// <returns>部门列表</returns>
        [HttpGet("GetDepartmentOptions")]
        public async Task<IActionResult> GetDepartmentOptions()
        {
            try
            {
                var departments = await _departmentService.GetDepartmentOptionsAsync();
                return Ok(new { success = true, data = departments, message = "查询成功" });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        #endregion

        #region 货品相关API

        /// <summary>
        /// 获取货品选项
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <returns>货品列表</returns>
        [HttpGet("GetProductOptions")]
        public async Task<IActionResult> GetProductOptions(string keyword = null, int pageIndex = 1, int pageSize = 10)
        {
            try
            {
                var result = await _productService.GetProductOptionsAsync(keyword, pageIndex, pageSize);
                return Ok(new { success = true, data = result.Data, totalCount = result.TotalCount, message = "查询成功" });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 根据货品ID列表批量获取货品信息
        /// </summary>
        /// <param name="productIds">货品ID列表</param>
        /// <returns>货品信息列表</returns>
        [HttpPost("GetProductsByIds")]
        public async Task<IActionResult> GetProductsByIds([FromBody] List<long> productIds)
        {
            try
            {
                var products = await _productService.GetProductsByIdsAsync(productIds);
                return Ok(new { success = true, data = products, message = "查询成功" });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        #endregion

        #region 单位相关API

        /// <summary>
        /// 获取单位选项
        /// </summary>
        /// <returns>单位列表</returns>
        [HttpGet("GetUnitOptions")]
        public async Task<IActionResult> GetUnitOptions()
        {
            try
            {
                var units = await _unitService.GetUnitOptionsAsync();
                return Ok(new { success = true, data = units, message = "查询成功" });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        #endregion

        #region 进货订单相关API

        /// <summary>
        /// 分页查询进货订单
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderCode">进货单号</param>
        /// <param name="status">单据状态</param>
        /// <param name="supplierId">供应商ID</param>
        /// <param name="orderDate">进货日期</param>
        /// <returns>分页结果</returns>
        [HttpGet("GetPurchaseOrderPageList")]
        public async Task<IActionResult> GetPurchaseOrderPageList(
            int pageIndex = 1,
            int pageSize = 10,
            string orderCode = null,
            int? status = null,
            long? supplierId = null,
            DateTime? orderDate = null)
        {
            try
            {
                var result = await _purchaseOrderService.GetPurchaseOrderPageListAsync(
                    pageIndex, pageSize, orderCode, status, supplierId, orderDate);

                if (result.Status)
                {
                    return Ok(result);
                }
                else
                {
                    return Ok(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 获取进货订单明细
        /// </summary>
        /// <param name="orderCode">进货单号</param>
        /// <returns>订单明细列表</returns>
        [HttpGet("GetOrderItems")]
        public async Task<IActionResult> GetOrderItems(string orderCode)
        {
            try
            {
                var items = await _purchaseOrderItemService.GetItemsByOrderCodeAsync(orderCode);
                return Ok(new { success = true, data = items, message = "查询成功" });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"查询失败：{ex.Message}" });
            }
        }

        /// <summary>
        /// 保存进货订单
        /// </summary>
        /// <param name="request">进货订单保存请求</param>
        /// <returns>保存结果</returns>
        [HttpPost("SavePurchaseOrder")]
        public async Task<IActionResult> SavePurchaseOrder([FromBody] SavePurchaseOrderRequest request)
        {
            try
            {
                var result = await _purchaseOrderService.SavePurchaseOrderAsync(request);
                if (result.Status)
                {
                    return Ok(new { success = true, message = result.Message, data = result.Data });
                }
                else
                {
                    return Ok(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"保存失败：{ex.Message}" });
            }
        }

        #endregion
        
    }

}
